<template>
  <div>
    <el-tree
      ref="tree"
      show-checkbox
      default-expand-all
      check-strictly
      :data="list"
      :props="{label:'name'}"
      node-key="id"
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>

<script>
import { tranListToTreeList } from '@/utils/tranListToTreeList'
import { getPermissionList } from '@/api/permission'
import { getRoleDetail, assignPrem } from '@/api/settings'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    // }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async  hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPrem({ id: this.roleId, permIds: permIds })
        console.log(res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        const perIds = await getRoleDetail(this.roleId)
        console.log(perIds, 'perIds')
        console.log(res)
        // 使用setCheckedKeys 选中角色拥有的权限点
        this.$refs.tree.setCheckedKeys(perIds.data.permIds)
        this.list = tranListToTreeList(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
